class LoaiTsController < ApplicationController
  # GET /loai_ts
  # GET /loai_ts.json
  def index
    @per_page = Param.get_value("itemsperpage")
    @search = LoaiT.where('TenLoai LIKE ?', "%#{params[:search]}%")
    @loai_ts = @search.paginate(:page => params[:page], :per_page => @per_page)

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @loai_ts }
      format.csv { send_data LoaiT.to_csv(@search) }
      format.xls
    end
  end

  # GET /loai_ts/1
  # GET /loai_ts/1.json
  def show
    @loai_t = LoaiT.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @loai_t }
    end
  end

  # GET /loai_ts/new
  # GET /loai_ts/new.json
  def new
    if sign_in?
      @loai_t = LoaiT.new

      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @loai_t }
      end
    else
      render 'sessions/deny'
    end
  end

  # GET /loai_ts/1/edit
  def edit
    if sign_in?
      @loai_t = LoaiT.find(params[:id])
    else
      render 'sessions/deny'
    end
  end

  # POST /loai_ts
  # POST /loai_ts.json
  def create
    @loai_t = LoaiT.new(params[:loai_t])

    respond_to do |format|
      if @loai_t.save
        format.html { redirect_to @loai_t, notice: 'Loai t was successfully created.' }
        format.json { render json: @loai_t, status: :created, location: @loai_t }
      else
        format.html { render action: "new" }
        format.json { render json: @loai_t.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /loai_ts/1
  # PUT /loai_ts/1.json
  def update
    @loai_t = LoaiT.find(params[:id])

    respond_to do |format|
      if @loai_t.update_attributes(params[:loai_t])
        format.html { redirect_to @loai_t, notice: 'Loai t was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @loai_t.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /loai_ts/1
  # DELETE /loai_ts/1.json
  def destroy
    @loai_t = LoaiT.find(params[:id])
    @loai_t.destroy

    respond_to do |format|
      format.html { redirect_to loai_ts_url }
      format.json { head :no_content }
    end
  end

  # Tạo đường dẫn để import
  def import
    if sign_in?
      @ts_import = Phong.new
      respond_to do |format|
        format.html
      end
    else
      render 'sessions/deny'
    end
  end

  # Tạo đường dẫn để import
  def save
    if (LoaiT.import(params[:file]))
      redirect_to import_loai_ts_path, notice: t("flash.import_msg")
    else
      redirect_to import_loai_ts_path, notice: t("flash.import_error")
    end
  end
end
